package com.zhang.hadoop.netty.nio.channel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author: zhang yufei
 * @create: 2022-01-09 17:00
 * @description: transferFrom快速拷贝
 **/
public class NIOFileChannel04 {
    public static void main(String[] args) {
        try {
            FileInputStream fileInputStream = new FileInputStream("picture01.jpg");
            FileOutputStream fileOutputStream = new FileOutputStream("picture02.jpg");

            //获取各个流对应fileChannel
            FileChannel sourceChannel = fileInputStream.getChannel();
            FileChannel destinationChannel = fileOutputStream.getChannel();

            //使用transferFrom完成拷贝
            destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());

            //关闭相关通道和流
            sourceChannel.close();
            destinationChannel.close();
            fileInputStream.close();
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
